#!/usr/bin/python

import urwid
from urwid.widget import FlowWidget, BoxWidget
from urwid.canvas import CanvasCombine

class Header(BoxWidget):
	"""Header contains two lines, one for text, one for divider"""
	
	ignore_focus = True
	
	def __init__(self, text="", root=None):
		self.root = root
		lines = text.splitlines(False)
		self.text = urwid.Text(lines[0])
		self.div  = urwid.Divider("-")
	
	def set_text(self, text):
		lines = text.splitlines(False)
		self.text.w.set_text(lines[0])

	def rows(self, size, focus=False):
		"""
		Return the number of lines that will be rendered.
			>>> Header("hello word").rows((10,))
				1
		"""
		(maxcol,) = size
		return 2
	
	def render(self, size, focus=False):
		(maxcol,) = size
		combine_list = []
		
		canv = self.text.render((maxcol,), focus)
		combine_list.append((canv, 0, False))
		canv = self.div.render((maxcol,), focus)
		combine_list.append((canv, 1, False))
		return CanvasCombine(combine_list)
		
# for test
if __name__ == "__main__":
	import urwid.raw_display
	import urwid

	def run():
		cols, rows = ui.get_cols_rows()
		rows = 2
		ui.register_palette( [
			('banner', 'light green', 'dark blue', ('standout', 'underline')),
			('bg', 'black', 'dark blue'),
			] )
		hdr = Header(text="Linux 2.6.14-selinux-cgl-afore")
		hdr = urwid.AttrWrap(hdr, 'banner')
		fill = urwid.Filler( hdr, 'top' )
		
		canvas = fill.render( (cols, rows) )
		ui.draw_screen( (cols, rows), canvas )
	
		while not ui.get_input():
			pass

	ui = urwid.raw_display.Screen()
	ui.run_wrapper( run )
	
		